/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.web.util;

import com.google.gson.Gson;
import java.util.List;
import java.util.Map;
import java.util.Set;

import java.lang.reflect.Method;
import java.util.Map.Entry;

/**
 *
 * @author Administrator
 */
public class JsonUtil {

    //
    public static String JavaMapToJson(Map<String, Object> map) {
        /*StringBuilder sb = new StringBuilder();
        sb.append("{");
        Set<Entry<String, Object>> entrySet = map.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            //System.out.println("****************"+key+"entry:"+entry);
            Object o = entry.getValue();
            String value = null;
            if (o == null) {
                value = "";
            } else {
                value = o.toString();
            }
            if (o instanceof Boolean || o instanceof Long || o instanceof Integer || o instanceof Double) {
                sb.append("\"").append(key).append("\"").append(":").append(value);
            } else {
                sb.append("\"").append(key).append("\"").append(":").append("\"").append(value).append("\"");
            }
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("}");
        return sb.toString();*/
        Gson gson = new Gson(); 
        return gson.toJson(map);  
    }

    public static String createSuccessJsonResponse(String msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("{success:true,msg:\"").append(msg).append("\"}");
        return sb.toString();
    }

    public static String createSuccessObjectJsonResponse(String jsonObject) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"success\":true,msg:\"").append(jsonObject).append("\"}");
        return sb.toString();
    }

    public static String createSuccessFileJsonResponse(String fileName) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"success\":true,\"filename\":\"").append(fileName).append("\"}");
        return sb.toString();
    }

    public static String createFailureJsonResponse(String msg) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"success\":false,\"code\":500,\"msg\":\"").append(msg).append("\"}");
        return sb.toString();
    }

    public static String JavaListToJson(List<?> list) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Object obj : list) {
            String objJson = JavaObjectToJson(obj);
            sb.append(objJson);
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("]");
        return sb.toString();
    }

    public static String JavaMapListToJson(List<Map<String, Object>> list) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Map obj : list) {
            String objJson = JavaMapToJson(obj);
            sb.append(objJson);
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("]");
        return sb.toString();
    }

    public static String JavaListToGridFormat(String root, List<?> list, int total) {
        StringBuilder sb = new StringBuilder();
        String listJson = JavaListToJson(list);
        //sb.append("{\"count\":").append(list.size()).append(",");
        sb.append("{\"count\":").append(total).append(",");
        sb.append("\"").append(root).append("\":");
        sb.append(listJson).append("}");
        return sb.toString();
    }

    public static String JavaMapListToGridFormat(String root, List<Map<String, Object>> list, int total) {
        StringBuilder sb = new StringBuilder();
        String listJson = JavaMapListToJson(list);
        //sb.append("{\"count\":").append(list.size()).append(",");
        sb.append("{\"count\":").append(total).append(",");
        sb.append("\"").append(root).append("\":");
        sb.append(listJson).append("}");
        return sb.toString();
    }

    //可以在第一层添加其它属性
    public static String JavaMapListToGridFormat(String root, List<Map<String, Object>> list, int total, String otherProperties) {
        StringBuilder sb = new StringBuilder();
        String listJson = JavaMapListToJson(list);
        //sb.append("{\"count\":").append(list.size()).append(",");
        sb.append("{\"count\":").append(total).append(",");
        if (otherProperties != null && !"".equals(otherProperties)) {
            sb.append(otherProperties).append(",");                   //其它属性
        }
        sb.append("\"").append(root).append("\":");
        sb.append(listJson).append("}");
        return sb.toString();
    }

    /**
     * *
     * 将数组 展开 转换成json格式片段. 如 int[] data， 数组大小为5，则转换成
     * data0:'value0',data1:'value1',data2:'value2',data3:'value3',data4:'value4'
     *
     * @return
     */
    public static String arrayToJsonlet(double[] objs, String name) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (Object obj : objs) {
            sb.append("\"").append(name).append(i++).append("\"").append(":").append("\"").append(obj.toString()).append("\"");
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String JavaObjectToJson(Object obj) {
        String propName;
        String propValue = null;
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        //Method[] allMethods = obj.getClass().getDeclaredMethods();
        Method[] allMethods = obj.getClass().getMethods();
        for (Method method : allMethods) {
            if (method.getName().startsWith("get")) {

                propName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
                if (propName.equals("class")) {
                    continue;
                }
                try {
                    Object objReturn = method.invoke(obj, new Object[0]);
                    if (objReturn instanceof double[]) {
                        sb.append(arrayToJsonlet((double[]) objReturn, propName)).append(",");
                    } else {
                        if (objReturn == null) {
                            propValue = "";
                        } else {
                            propValue = objReturn.toString();
                        }
                        sb.append("\"").append(propName).append("\":\"").append(propValue).append("\",");
                    }
                } catch (Exception e) {
                }
            }
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("}");
        return sb.toString();
    }
    
    
    //
    public static String toHighChartsData(List<Map<String,Object>> list,String paraName){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for(Map<String,Object> map:list){
            Object o = map.get(paraName);
            sb.append(o.toString());
            sb.append(",");
        }
        if (sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("]");
        return sb.toString();
    }
}
